# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
import os

def write(url):
   tree = ET.parse(url)  # 将XML文件转换为ElementTree能解析的格式
   root = tree.getroot()  # 获取根元素
   f = "danmu.txt" #写入的文件
   if os.path.getsize(url):
       w = open(f, 'w', encoding='utf-8')
   else:
       w = open(f, 'a', encoding='utf-8')

   for d in root.findall("d"):   #找到所有弹幕节点
     d=d.text
     w.write(d + ' ' )

     print(d)
   w.close() #关闭文件

def text(url):
    tree = ET.parse(url)  # 将XML文件转换为ElementTree能解析的格式
    root = tree.getroot()  # 获取根元素
    danmu_arrray = []
    for d in root.findall("d"):   #找到所有弹幕节点
      d=d.text
      danmu_arrray.append(d)
    string=' '.join(danmu_arrray)
    print(string)
    return string



#write('comments/comment_static.xml')

# text('comments/comment_static.xml')












